<%@page import="java.io.ByteArrayInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%
String filePath = request.getParameter( "filePath" );
String fileName = request.getParameter( "fileName" );
String message = request.getParameter( "message" );
message = message==null?"":message.trim();

ServletOutputStream fout = null;

try{
    File file = null;
    int fsize = 0;
    if(message.equals("")){
    	file = new File( filePath );
    	fsize = (int) file.length();
    }else{
    	fsize = (int) message.getBytes().length;
    }
    
    response.setHeader( "Content-Disposition" , "attachment;filename="+fileName+";"  );
    response.setContentType( "Content-Type : application/octect-stream" );
    response.setContentLength( fsize );

    out.clear();
    out = pageContext.pushBody();

    fout = response.getOutputStream();
    InputStream fin = message.equals("")?new FileInputStream( file ):new ByteArrayInputStream( message.getBytes() );

    int read;
    byte readByte[] = new byte[1024];
    
    while ((read = fin.read(readByte, 0, 1024)) != -1) {  
    	fout.write(readByte, 0, read);  
    } 


}catch(Exception e1){e1.printStackTrace();}
finally{
    try{
        if(fout != null){
            fout.close();
        }
    }catch(Exception e2){e2.printStackTrace();}
}

%>